*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {

*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "log_R_esp_er_hp " 
local lava ""Lending rate changes" " 
local yti ""Percentage pts"" 

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'

*-------------------------------------------------------------------------------------------------------------

local count = 0
foreach v of local vars {
local count = `count'+1
n display "variable `v'"

*_________ GET DATA ____________
use "`path'data/working files/prepared.dta", clear 

*____ DECLARE TIME SERIES _____
gen id =1
sort year
tsset id year, yearly


*_________________ LOCAL PROJECTIONS _________________



	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
		gen irf_`v' = . if _n<=2*`H'+1
		gen se_`v' = . if _n<=2*`H'+1

		gen ci_ub_`v' = . if _n<=2*`H'+1
		gen ci_lb_`v' = . if _n<=2*`H'+1
		gen ci_ub90_`v' = . if _n<=2*`H'+1
		gen ci_lb90_`v' = . if _n<=2*`H'+1

	***_________ ESTIMATE COEFFICIENTS
	
	local counth = 0
	forvalues y = 0/`H' {
		local counth = `counth'+1
		n display "projection `y'"
		

		newey l`y'.`v'_gr  f(0/`L').defaults f(0/`L').temperature f(0/`L').warbeu f(0/`L').warbciv f(1/`L').gdp_r_adj_gr f(1/`L').cpi_sil_gr f(1/`L').wage_sil_gr f(1/`L').itsN_gr, lag(5) force 
				
		* Shock response
			lincom defaults 
			replace irf_`v' = r(estimate) if _n==`H'+2-`counth' /* coefficients */
			replace se_`v' = r(se) if _n==`H'+2-`counth' 
			
			newey f`y'.`v'_gr  l(0/`L').defaults l(0/`L').temperature l(0/`L').warbeu l(0/`L').warbciv l(1/`L').gdp_r_adj_gr l(1/`L').cpi_sil_gr l(1/`L').wage_sil_r_gr l(1/`L').itsN_gr, lag(5) force   
			
			* Shock response
			lincom defaults 
			replace irf_`v' = r(estimate) if _n==`H'+`counth'  /* coefficients */
			replace se_`v' = r(se) if _n==`H'+`counth'
		} //horizon			

	
replace ci_ub_`v' = irf_`v' + 1*se_`v' if _n <= 2*`H'+1
replace ci_lb_`v' = irf_`v' - 1*se_`v' if _n <= 2*`H'+1
replace ci_ub90_`v' = irf_`v' + 1.64*se_`v' if _n <= 2*`H'+1
replace ci_lb90_`v' = irf_`v' - 1.64*se_`v' if _n <= 2*`H'+1


* plot prerequisites
gen time = _n - `H'-1 if(_n<=2*`H'+1) 
drop if time == . 
gen zero = 0 if time!=.

local title : word `count' of `lava'
local ytitle : word `count' of `yti'
local lsize "medlarge"
local tsize "medlarge"

twoway  (rarea ci_ub90_`v' ci_lb90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_ub_`v' ci_lb_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Years from default", size(`lsize')) ytitle(`ytitle', height(2) size(`lsize')) xlabel(-5/5,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize'))  ///
		legend(off) title(, size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw
			


} // vars

graph display g1, ysize(2) xsize(3)
graph export "results/FigureA6.pdf", replace 	

}


*_________________________________________

			
